/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.app.model.GeneratorConfig;
import com.xw.app.service.GeneratorConfigService;
import com.xw.app.vo.query.GeneratorConfigQuery;
import com.xw.framework.base.controller.BaseSpringController;
import com.xw.util.BeanUtils;

/**
 * The Class GeneratorConfigController.
 *
 * @author Xi Wei
 */
@Controller
@RequestMapping("/generatorConfig")
public class GeneratorConfigController extends BaseSpringController {

	@Autowired
	private GeneratorConfigService generatorConfigService;
	
	/**
	 * List.
	 *
	 * @param request the request
	 * @param query the query
	 * @return the model and view
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,GeneratorConfigQuery query){
		Page<GeneratorConfig> page = generatorConfigService.findPage(query);
		request.getSession().setAttribute(GeneratorConfig.class.getSimpleName(), query);
		ModelAndView result = new ModelAndView("/GeneratorConfig/list");
		ModelMap map = toModelMap(page, query);
		result.addAllObjects(map);
		return result;		
	}
	
	/**
	 * Creates the.
	 *
	 * @param generatorConfig the generator config
	 * @return the model and view
	 */
	@RequestMapping("/popup/create")
	public ModelAndView create(GeneratorConfig generatorConfig){
		ModelAndView result = new ModelAndView("/GeneratorConfig/add");
		ModelMap map = new ModelMap();
		if(generatorConfig.getId() != null){
			generatorConfig = generatorConfigService.getById(generatorConfig.getId());
		}
		map.addAttribute("generatorConfig", generatorConfig);
		result.addAllObjects(map);
		return result;
	}
	
	
	/**
	 * Save.
	 *
	 * @param generatorConfig the generator config
	 * @param validateResult the validate result
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping("/popup/save")
	public ModelAndView save(@Valid GeneratorConfig generatorConfig,BindingResult validateResult,HttpServletRequest request){
		if(validateResult.hasErrors()){
			return create(generatorConfig);
		}
		
		if(generatorConfig.getId() != null){
			GeneratorConfig existing = generatorConfigService.getById(generatorConfig.getId());
			BeanUtils.copyProperties(existing, generatorConfig);
		}
		generatorConfigService.save(generatorConfig);
		ModelMap map = new ModelMap();
		ModelAndView result = new ModelAndView("/saveResult");
		map.addAttribute("msg", "success");
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Check config name.
	 *
	 * @param configName the config name
	 * @param id the id
	 * @return the boolean
	 */
	@RequestMapping("/json/checkConfigName")
	@ResponseBody
	public Boolean checkConfigName(String configName,Long id){
		GeneratorConfig generatorConfig = new GeneratorConfig();
		generatorConfig.setConfigName(configName);
		generatorConfig.setId(id);
		if(generatorConfigService.checkConfigNameExist(generatorConfig)){
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}
}
